<?php
class Application_Model_InformationMapper
{
    protected $_dbTable;
    public function setDbTable ($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    public function getDbTable ()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Information');
        }
        return $this->_dbTable;
    }
    /**
     * 
     * Save one instance of the Application_Model_Information
     * @param Application_Model_Inforamtion $information
     */
    public function save (Application_Model_Informaiton $information)
    {
        $data = array('FID' => $information->getFid(), 
        'Title' => $information->getTitle(), 
        'Content' => $information->getContent(), 
        'Time' => $information->getTime(), 
        'Publisher' => $information->getPublisher(), 
        'Contact' => $information->getContact(), 
        'Class' => $information->getCalss(), 'Pay' => $information->getPay(), 
        'Review' => $information->getReview());
        if (null === ($id = $information->getFid())) {
            unset($data['FID']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('FID = ?' => $id));
        }
    }
    public function find ($id, Application_Model_Information $information)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $information->setFid($row->FID)
            ->setTitle($row->Title)
            ->setContent($row->Content)
            ->setTime($row->Time)
            ->setPublisher($row->Publisher)
            ->setContact($row->Contact)
            ->setClass($row->Class)
            ->setPay($row->Pay)
            ->setReview($row->Review);
    }
    public function fetchAll ()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Information();
            $entry->setFid($row->FID)
                ->setTitle($row->Title)
                ->setContent($row->Content)
                ->setTime($row->Time)
                ->setPublisher($row->Publisher)
                ->setContact($row->Contact)
                ->setClass($row->Class)
                ->setPay($row->Pay)
                ->setReview($row->Review);
            $entries[] = $entry;
        }
        return $entries;
    }
    
    public function fetchByName ($column, $value)
    {
        $db = $this->getDbTable()->getAdapter();
        $where = $db->quoteInto($column . ' = ?', $value);
        $resultSet = $this->getDbTable()->fetchRow($where);
        $entries = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Information();
            $entry->setFid($row->FID)
                ->setTitle($row->Title)
                ->setContent($row->Content)
                ->setTime($row->Time)
                ->setPublisher($row->Publisher)
                ->setContact($row->Contact)
                ->setClass($row->Class)
                ->setPay($row->Pay)
                ->setReview($row->Review);
            $entries[] = $entry;
        }
        return $entries;
    }
}

